package com.hjx.windowtool.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hjx.windowtool.bk.dibaqu.server.BookChapter;
import com.hjx.windowtool.bk.dibaqu.server.BookInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 第八区
 * 
 * @author HuangJianxiong 2020年5月25日 上午10:31:32
 */
@Slf4j
public class XiaoshuoDibaquDownloadTest {
	private static final ExecutorService executorService = Executors.newFixedThreadPool(10);
	private static String info;

	public static void main(String[] args) throws Exception {

		// String s="01232阿达萨达";
		// boolean matches = s.matches(".*[0-9]+.*");
		// System.out.println(matches);
		//
		//
		 String dir = "E:\\book\\12";
//		 packageFile(dir, "无尽穿梭");
		
		 String fileName = "千万人";
		 String url = "http://www.dibaqu123.com/18/18153/";
		//
		 download(dir, fileName, url);

//		List<BookInfo> list = getBookList(1);
//		getChapter(list.get(0));
//		System.out.println(list.get(0).getDetail());
		
		
	}

	/**
	 * @return
	 * @throws IOException
	 */
	public static List<BookInfo> getBookList(int page) throws IOException {
		Connection conn = Jsoup.connect("http://www.dibaqu123.com/book/allvote-"+page+".html");
		conn.header("User-Agent", XiaoShuoTest2.userAgent);
		conn.header("Referer", "http://www.dibaqu123.com");
		Document d = conn.get();
//		System.out.println(d);
		Elements select = d.select(".article").select("li");
		List<BookInfo> list=new ArrayList<>();
		if (select.isEmpty()) {
			return list;
		}
		for (int i = 0; i < select.size(); i += 5) {
			Element e = select.get(i);
			// System.out.println("==="+e);
			BookInfo b = new BookInfo();
			b.setTitle(e.text().trim());
			String url = e.select("a").attr("abs:href");
			b.setUrl(url);
			b.setLastchapter(select.get(i + 1).text().trim());
			b.setAuthor(select.get(i + 2).text().trim());
			b.setLastUpdate(StrUtil.parseDate(select.get(i + 3).text().trim()));
			b.setFullflag(select.get(i + 4).text().trim());
			list.add(b);
		}
		return list;
	}
	
	public static void getChapter(BookInfo b) throws IOException, InterruptedException {
		String url=b.getUrl();
		Connection conn1 = Jsoup.connect(url);
		conn1.header("User-Agent", XiaoShuoTest2.userAgent);
		conn1.header("Referer", "http://www.dibaqu123.com");
		Document d1 = conn1.get();
//		System.out.println(d1);
//		String titlePage = d1.select(".info1").select("img").attr("abs:src");
//		System.out.println(titlePage);
		
//		String type = d1.select(".breadcrumb").select("li").get(1).select("a").text();
//		System.out.println(type);
		
		Elements select = d1.select(".list-charts");
		if(select.isEmpty()) {
			return;
		}
		String detail = d1.select(".pannel-body").select("p").text();
//		System.out.println(detail);
		b.setDetail(detail);
		Element element = select.get(0);
		Elements select1 = element.select("a");
		List<BookChapter> listTitle = new ArrayList<>();
		log.debug("获取:{}-{} 详情",b.getTitle(),url);
		for (Element e1 : select1) {
			BookChapter bc = new BookChapter();
			bc.setTitle(e1.text());
			bc.setUrl(e1.attr("abs:href"));
			if(bc.getTitle()!=null&&bc.getTitle().contains("T")) {
				bc.setType("图片");
			}
			listTitle.add(bc);
		}
		log.debug("获取到数量：{}", listTitle.size());
		b.setChapters(listTitle);
		b.setChapterCount(listTitle.size());
		Thread.sleep(1000);
	}

	/**
	 * @param dir
	 * @param fileName
	 * @param url
	 * @return 
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public static String download(String dir, String fileName, String url) throws IOException, UnsupportedEncodingException {
		getContentUrls(dir, url);
		loadBookContentDibaqu(dir);
		return packageFile(dir, fileName);
	}

	/**
	 * @param dir
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static List<String> getContentUrls(String dir, String url) throws IOException {
		if (!new File(dir).exists() || !new File(dir).isDirectory()) {
			new File(dir).mkdirs();
		}
		Connection conn = Jsoup.connect(url);
		conn.header("User-Agent", XiaoShuoTest2.userAgent);
		conn.header("Referer", "http://www.dibaqu123.com");
		Document d = conn.get();
		System.out.println(d);
		info = d.select(".info2").text();

		Element element = d.select(".list-charts").get(0);
		System.out.println(element);
		// Element element = d.select("#list-chapterAll").get(0);
		Elements select = element.select("a");
		List<String> listUrl = new ArrayList<>();
		List<String> listTitle = new ArrayList<>();
		for (Element e : select) {
			// System.out.println(e.attr("abs:href")+"==="+e.text());
			listUrl.add(e.attr("abs:href"));
			listTitle.add(e.text());
		}
		Files.write(Paths.get(dir + "//url.text"), listUrl);
		Files.write(Paths.get(dir + "//titel.text"), listTitle);
		log.info("获取到{}章", listTitle.size());
		return listTitle;
	}

	/**
	 * @param dir
	 * @param fileName
	 * @param listTitle
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public static String packageFile(String dir, String fileName) throws IOException, UnsupportedEncodingException {
		List<String> listTitle = Files.readAllLines(new File(dir, "titel.text").toPath());
		if (Files.exists(new File(dir, listTitle.size() - 1 + ".txt").toPath())) {
			List<String> list = Files.readAllLines(new File(dir, "titel.text").toPath());
			File file = new File(dir, fileName + ".txt");
			try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
				bw.write(info);
				bw.newLine();
				int num = 1;
				for (int i = 0; i < list.size(); i++) {
					String string = list.get(i);
					if (!new String(Files.readAllBytes(new File(dir, i + ".txt").toPath())).contains(string.trim().split(" ")[0])) {
						if (string.contains("章") || string.contains("节")) {

						} else if (string.matches(".*[0-9]+.*")) {
							bw.write((num++) + "章");
						}
						bw.write(string);
						bw.newLine();
					}
					List<String> readAllLines = Files.readAllLines(new File(dir, i + ".txt").toPath());
					for (String string2 : readAllLines) {
						if (string2 == null || string2.trim().isEmpty()) {
							continue;
						}
						bw.write(string2);
						bw.newLine();
					}
				}
			}
			return file.getAbsolutePath();
		} else {
			return XiaoShuoTest2.packageChm(dir, fileName);
		}
	}

	/**
	 * @param dir
	 * @throws IOException
	 */
	public static void loadBookContentDibaqu(final String dir) throws IOException {
		final List<String> listUrl = Files.readAllLines(Paths.get(dir + "//url.text"));
		for (int j = 0; j < listUrl.size(); j++) {
			final int i = j;
			Runnable runnable = new Runnable() {
				public void run() {
					while (true) {
						String url = listUrl.get(i);
						try {
							log.info("获取第{}条数据：{}", i, url);
							Connection conn = Jsoup.connect(url);
							conn.header("User-Agent", XiaoShuoTest2.userAgent);
							Document d = conn.get();
							// System.out.println(d);
							Elements select = d.select(".imagecontent");
							if (select.size() > 0) {
								String attr = select.attr("src");
								log.info("获取到图片：{}", attr);
								XiaoShuoTest2.downloadImage(attr, dir + "//" + i + ".gif", url);
							} else {
								List<String> list = new ArrayList<>(Arrays.asList(d.select("#nr").html().split("<br>")));
								if (list.size() >= 2) {
									list.remove(list.size() - 1);
								}
								if (list.size() > 0) {
									list.remove(0);
								}
								List<String> listContents = new ArrayList<>();
								for (String string : list) {
									listContents.add(string.replaceAll("&nbsp;", " "));
								}
								Files.write(Paths.get(dir + "//" + i + ".txt"), listContents);
								log.info("{}保存文本成功", i);
							}
							break;
						} catch (Exception e) {
							log.error("获取：{}-{}数据失败：{}", i, url, e);
						} finally {
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {

							}
						}
					}
				}
			};
			executorService.submit(runnable);
		}
		executorService.shutdown();
		while (true) {
			if (executorService.isTerminated()) {
				log.info("数据下载完成");
				break;
			}
		}
	}

	static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
			return;
		}

		public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
			return;
		}
	}
}
